

import { Body, Controller, Get,Post,Query } from '@nestjs/common';
import { CartService } from 'src/services/cart/cart.service'
import ICart from 'src/types/cart.interface';
import HttpResponse from 'src/types/http.response.interface';

type UnionType = ICart[] | ICart;



@Controller('cart')
export class CartController {
      //等价于上面的写法
      constructor(private readonly  cartService:CartService){

      }
      @Get()  
      async getAllCartInfo():Promise<HttpResponse<ICart[]>>{
         const data = await this.cartService.getAllData();
         console.log(`得到的购物车数据-${Date.now()*1}`,data)
         if(Array.isArray(data)){
          return {
              success:true,
              message:"查询数据成功",
              data,
          }
         }
         else{
          return {
              success:false,
              message:"查询数据失败",
              data:[],
          }
         }
         
      }
      /**
     * 用来通过postman或者insomnia来导入数据
     * 在浏览器中输入http://localhost:9898/cart/importData
     */
    
    @Post('/importData')
    //DTO Data Transfer Object 数据传输对象，当客户端把数据传送到后台的
    async importData(@Body() requestParamsBody:UnionType) {
      
        const data:UnionType = await this.cartService.importData(requestParamsBody);
        return {
            success:true,
            message:"数据导入成功",
            data
        };
    }
}




